/**
 * 
 * json 辅助类
 * 
 */
package com.fwzc.giuseppe.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;


/**
 * json 辅助类
 */
public class Tool {

	/**
	 * JSON字符串 转成 JSONObject
	 * @param s
	 * @return JSONObject
	 */
	public static JSONObject getJsonObject(String s) {
		try {
			JSONObject obj = new JSONObject(s);
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * JSON字符串 转成 JSONArray
	 * @param s
	 * @return JSONArray
	 */
	public static JSONArray getJsonArray(String s) {
		try {
			JSONArray arr = new JSONArray(s);
			return arr;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}
	
	
	/**
	 * 从JSON字符串中通过key获取JSONArray
	 * @param obj JSONObject
	 * @param s key
	 * @return JSONArray
	 */
	public static JSONArray getJsonArray(String json, String s) {
		try {
			
			JSONObject obj=Tool.getJsonObject(json);
			JSONArray arr = obj.getJSONArray(s);
			return arr;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 从JSONObject中通过key获取JSONArray
	 * @param obj JSONObject
	 * @param s key
	 * @return JSONArray
	 */
	public static JSONArray getJsonArray(JSONObject obj, String s) {
		try {
			JSONArray arr = obj.getJSONArray(s);
			return arr;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}
		
	
	/**
	 * 从JSON字符串中通过key获取int
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return int
	 */
	public static int getInt(String json, String s) {
		try {
			JSONObject obj=Tool.getJsonObject(json);
			int n = obj.getInt(s);
			return n;
		} catch (JSONException e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 从JSON字符串中通过key获取double
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return double
	 */
	public static double getDouble(String json, String s) {
		try {
			JSONObject obj=Tool.getJsonObject(json);
			double n = obj.getDouble(s);
			return n;
		} catch (JSONException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	
	/**
	 * 从JSON字符串中通过key获取float
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return float
	 */
	public static float getFloat(String json, String s) {
		float n = ParseUtils.string2float(Tool.getString(json, s));
		return n;
	}
	
	/**
	 * 从JSON字符串中通过key获取floatString
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return String
	 */
	public static String getFloatString(String json, String s) {
		String n = new BigDecimal(ParseUtils.string2floats(Tool.getString(json, s))).setScale(2, BigDecimal.ROUND_HALF_UP)+"";
		return n;
	}
	
	
	
	/**
	 * 从JSON字符串中通过key获取String
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return String
	 */
	public static String getString(String json, String s) {
		try {
			JSONObject obj=Tool.getJsonObject(json);
			String str = obj.getString(s);
			if(str.equals("null")){
				str="";
			}
			return str;

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	

	/**
	 * 从JSONObject中通过key获取int
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return int
	 */
	public static int getInt(JSONObject obj, String s) {
		try {
			int n = obj.getInt(s);
			return n;
		} catch (JSONException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	
	/**
	 * 从JSON字符串中通过key获取float
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return float
	 */
	public static float getFloat(JSONObject obj, String s) {
		float n = ParseUtils.string2float(Tool.getString(obj, s));
		return n;
	}
	
	/**
	 * 从JSON字符串中通过key获取floatString
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return String
	 */
	public static String getFloatString(JSONObject obj, String s) {
		String n = ParseUtils.string2floats(Tool.getString(obj, s));
		return n;
	}
	
	/**
	 * 从JSONObject中通过key获取double
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return double
	 */
	public static double getDouble(JSONObject obj, String s) {
		try {
			double n = obj.getDouble(s);
			return n;
		} catch (JSONException e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 从JSONObject中通过key获取String
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return String
	 */
	public static String getString(JSONObject obj, String s) {
		try {
			String str = obj.getString(s);
			
			if(str.equals("null")){
				str="";
			}
			
			return str;

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 从JSONObject中通过key获取long
	 * 
	 * @param obj JSONObject
	 * @param s key
	 * @return String
	 */
	public static long getLong(JSONObject obj, String s) {
		try {
			long str = obj.getLong(s);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public static boolean getBoolean(JSONObject obj, String s){
		try {
			boolean str = obj.optBoolean(s);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 从JSONArray中通过索引获取String
	 * 
	 * @param arr JSONArray
	 * @param index 索引
	 * @return String
	 */
	public static String getStringFromArray(JSONArray arr, int index) {
		try {
			String str = arr.getString(index);
			return str;

		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 从JSONArray中通过索引获取JSONObject
	 * @param arr JSONArray
	 * @param index 索引
	 * @return JSONObject
	 */
	public static JSONObject getJSONObjectFromArray(JSONArray arr, int index) {
		try {
			JSONObject obj = (JSONObject) arr.get(index);
			return obj;

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 从JSONArray中通过索引获取JSONArray
	 * @param arr JSONArray
	 * @param index 索引
	 * @return JSONArray
	 */
	public static JSONArray getArrayFromArray(JSONArray arr, int index) {
		try {
			JSONArray arrt = arr.getJSONArray(index);
			return arrt;

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 从JSONObject中通过key获取JSONObject
	 * @param obj JSONObject
	 * @param s key
	 * @return JSONObject
	 */ 
	public static JSONObject getObject(JSONObject obj, String s) {
		try {
			JSONObject objt = obj.getJSONObject(s);
			return objt;

		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 将List转成JSONArray
	 * @param list list
	 * @return JSONArray
	 */ 
	public static JSONArray getArray(List<HashMap<String, String>> list) {
		
		JSONArray arr = new JSONArray();
		
		for (int i = 0; i < list.size(); i++) {
			
			HashMap<String, String> map=list.get(i);
			
			String str=map.toString();
			
			arr.put(str);
		}
		
//			arr=arr.
		return arr;
	}
	
	/**
	 * 组装JSONObject
	 * @param obj 要组装的JSONObject
	 * @param key key
	 * @param value value
	 * @return JSONObject
	 */
	public static JSONObject putObject(JSONObject obj,String key,String value){
		
		try {
			obj.put(key, value);
			return obj;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
	
	
	/**
	 * 组装JSONObject
	 * @param obj 要组装的JSONObject
	 * @param key key
	 * @param value value
	 * @return JSONObject
	 */
	public static JSONObject putArray(JSONObject obj,String key,JSONArray value){
		
		try {
			obj.put(key, value);
			return obj;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
	
	/**
	 * 从Object json中获取MAP数组
	 * 
	 * @param json
	 * @return ArrayList<HashMap<String, String>>
	 */
	public static ArrayList<HashMap<String, String>> getListMapByJson(Object json) {
		ArrayList<HashMap<String, String>> objs = new ArrayList<HashMap<String, String>>();
		JSONArray array = new JSONArray();
		try {
			if (json instanceof JSONArray)
				array = (JSONArray) json;
			else if (((String) json).length() == 0)
				return objs;
			else
				array = new JSONArray((String) json);
		} catch (JSONException e1) {
			try {
				array.put(new JSONObject((String) json));
			} catch (JSONException e2) {
				Log.e("Tool", "转换josn格式错误");
			}
		}
		for (int i = 0; i < array.length(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			try {
				Iterator<?> it = array.getJSONObject(i).keys();
				while (it.hasNext()) {
					String key = (String) it.next();
					map.put(key, array.getJSONObject(i).get(key).toString());
				}
			} catch (Exception e) {
				try {
					map.put("", array.get(i).toString());
				} catch (JSONException e1) {
					Log.e("Tool", "转换josn格式错误");
				}
			}
			objs.add(map);
		}
		return objs;
	}
	/**
	 * 从json字符串中获取HashMap
	 * 
	 * @param json
	 * @return HashMap<String, String>
	 */
	public static HashMap<String, String> getMapByJsonObj(String json) {
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			JSONObject jsonObj=Tool.getJsonObject(json);
			Iterator<?> it = jsonObj.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				if(jsonObj.getString(key).equals("null")){
					map.put(key, "");
				}else map.put(key, jsonObj.getString(key));
			}
		} catch (Exception e) {
			Log.e("Tool", "转换josn格式错误");
		}
		return map;
	}
	
	
	/**
	 * 从json字符串中获取HashMap再转为list
	 * 
	 * @param json
	 * @return HashMap<String, String>
	 */
	public static ArrayList<HashMap<String, String>> getMapByJsonObj2List(String json) {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		HashMap<String, String> map = getMapByJsonObj(json);
			
		Set<String> keySet = map.keySet(); // key的set集合
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			HashMap<String, String> maps = new HashMap<String, String>();
			String key = (String) it.next();
			maps.put("key", key);
			if(map.get(key).equals("null")){
				maps.put("value", "");
			}else {
				maps.put("value", map.get(key));
			}
			
			list.add(maps);
		}
		Collections.reverse(list);
		return list;
	}
	
	
	
	/**
	 * 从JSONObject中获取HashMap再转为list
	 * 
	 * @param json
	 * @return HashMap<String, String>
	 */
	public static ArrayList<HashMap<String, String>> getMapByJsonObj2List(JSONObject json) {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		HashMap<String, String> map = getMapByJsonObj(json);
			
		Set<String> keySet = map.keySet(); // key的set集合
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			HashMap<String, String> maps = new HashMap<String, String>();
			String key = (String) it.next();
			maps.put("key", key);
			if(map.get(key).equals("null")){
				maps.put("value", "");
			}else {
				maps.put("value", map.get(key));
			}
			
			list.add(maps);
		}
		Collections.reverse(list);
		return list;
	}
	
	
	/**
	 * 从json字符串中获取HashMap再转Stringlist
	 * 
	 * @param json
	 * @return HashMap<String, String>
	 */
	public static ArrayList<String> getMapByJsonObj2ValueList(String json) {
		ArrayList<String> list = new ArrayList<String>();
		
		HashMap<String, String> map = getMapByJsonObj(json);
			
		Set<String> keySet = map.keySet(); // key的set集合
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			HashMap<String, String> maps = new HashMap<String, String>();
			String key = (String) it.next();
			maps.put("key", key);
			if(map.get(key).equals("null")){
				list.add("");
			}else {
				list.add(map.get(key));
			}
			
		}
		Collections.reverse(list);
		return list;
	}
	
	/**
	 * 从JSONObject中获取HashMap
	 * 
	 * @param json
	 * @return  HashMap<String, String>
	 */
	public static HashMap<String, String> getMapByJsonObj(JSONObject json) {
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			Iterator<?> it = json.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				if(json.getString(key).equals("null")){
					map.put(key, "");
				}else map.put(key, json.getString(key));
			}
		} catch (Exception e) {
			Log.e("Tool", "转换josn格式错误");
		}
		return map;
	}


	/**
	 * 从Object json中获取MAP数组
	 * 
	 * @param json
	 * @return ArrayList<HashMap<String, String>>
	 */
	public static ArrayList<HashMap<String, String>> getListMapByJsonArray(Object json) {
		ArrayList<HashMap<String, String>> objs = new ArrayList<HashMap<String, String>>();
		JSONArray array = new JSONArray();
		try {
			if (json instanceof JSONArray)
				array = new JSONArray(json.toString());
			else if (json instanceof JSONObject)
				array.put(new JSONObject(json.toString()));

			for (int i = 0; i < array.length(); i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				Iterator<?> it = array.getJSONObject(i).keys();
				while (it.hasNext()) {
					String key = (String) it.next();
					if(array.getJSONObject(i).get(key).toString().equals("null")){
						map.put(key, "");
					}else map.put(key, array.getJSONObject(i).get(key).toString());
				}
				objs.add(map);
			}
		} catch (Exception ex) {
		}
		return objs;
	}
	/**
	 * 从json字符串中获取MAP数组
	 * 
	 * @param json
	 * @return ArrayList<HashMap<String, String>>
	 */
	public static ArrayList<HashMap<String, String>> getListMapByJsonArrayString(String json) {
		ArrayList<HashMap<String, String>> objs = new ArrayList<HashMap<String, String>>();
		JSONArray array = new JSONArray();
		try {
			array = new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				Iterator<?> it = array.getJSONObject(i).keys();
				while (it.hasNext()) {
					String key = (String) it.next();
					if(array.getJSONObject(i).get(key).toString().equals("null")){
						map.put(key, "");
					}else map.put(key, array.getJSONObject(i).get(key).toString());
				}
				objs.add(map);
			}
		} catch (Exception ex) {
		}
		return objs;
	}

	/**
	 * String 转换成 int
	 * 
	 * @param number
	 * @return
	 */
	public static int parseInt(String number) {
		try {
			int num = Integer.parseInt(number);
			return num;

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 将map转成json字符串（HashMap<String, String> map）
	 * @param map
	 * @return
	 */
	public static String postJson(HashMap<String, String> map){
		JSONObject json=new JSONObject();
		
		for (int i = 0; i < Utils.getkey(map).length; i++) {
			
			Tool.putObject(json,Utils.getkey(map)[i], Utils.getValues(map)[i]);
		}
		
		return json.toString();
	}
	
	
	/**
	 * 将map转成json字符串（包含HashMap<String, String>，HashMap<String, JSONArray>）
	 * @param map
	 * @param maps
	 * @return
	 */
	public static String postJson(HashMap<String, String> map,HashMap<String, JSONArray> maps){
		JSONObject json=new JSONObject();
		for (int i = 0; i < Utils.getkey(map).length; i++) {
			
			Tool.putObject(json,Utils.getkey(map)[i], Utils.getValues(map)[i]);
		}
		
		for (int i = 0; i <Utils.getkey(maps).length; i++) {
			
			Tool.putArray(json, Utils.getkey(maps)[i], maps.get(Utils.getkey(maps)[i]));
		}
		
		return json.toString();
	}
	
	
	/**
	 * 将map转成json字符串（HashMap<String, JSONArray>）
	 * @param maps
	 * @return
	 */
	public static String postArrayJson(HashMap<String, JSONArray> maps){
		JSONObject json=new JSONObject();
		
		for (int i = 0; i <Utils.getkey(maps).length; i++) {
			
			Tool.putArray(json, Utils.getkey(maps)[i], maps.get(Utils.getkey(maps)[i]));
		}
		
		return json.toString();
	}

}
